//
//  SCViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-24.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "SCViewController.h"
#import "NewsData.h"
#import "NewModel.h"
#import "NewsNRViewController.h"
#import "NewCell.h"
#import "UIImageView+WebCache.h"
@interface SCViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *arr;
@end

@implementation SCViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"我的收藏";
        self.arr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor whiteColor];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UITableView *tmpTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tmpTableView.dataSource = self;
    tmpTableView.delegate = self;
    [tmpTableView registerClass:[NewCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:tmpTableView];
    [tmpTableView release];
    self.myTableView = tmpTableView;
}


-(void)viewWillAppear:(BOOL)animated
{
    NewsData *newsData = [NewsData shareInstance];
    [newsData openDB];
    self.arr = [newsData selectAllNews];
    
    [self.myTableView reloadData];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, self.view.bounds.size.height) style:UITableViewStylePlain];
    
    [self.myTableView setDataSource:self];
    [self.myTableView setDelegate:self];
    [self.view addSubview:self.myTableView];
    [self.myTableView release];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arr count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *newsModel = [[NewModel alloc]init];
    newsModel = [self.arr objectAtIndex:indexPath.row];
    static NSString *str = @"reuse";
    NewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[NewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    cell.titleLabel.text = newsModel.title;
    cell.sumLabel.text = newsModel.summaryInfo;
    NSString *string = newsModel.image;
    NSURL *url = [NSURL URLWithString:string];
    [cell.myImageView setImageWithURL:url];
    
    //    cell.myImageView
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *newsModel = [[NewModel alloc]init];
    newsModel = [self.arr objectAtIndex:indexPath.row];
    NewsNRViewController *newsNRVC = [[NewsNRViewController alloc] init];
    newsNRVC.titleStr = newsModel.title;
    newsNRVC.imageStr = newsModel.image;
    newsNRVC.MyidStr = newsModel.Myid;
    newsNRVC.summaryInfoStr = newsModel.summaryInfo;
    [self.navigationController pushViewController:newsNRVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
